<template>
  <div class="addGoodsAdd">
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="topTitle"
      :show-close="true"
      :modal-append-to-body="false"
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <!-- 点击上面的新增   编辑 -->
      <div class="newAddEdit" style="margin: 0 auto; width: 80%">
        <div class="newAddConfig">
          <el-form
            :model="editCartGoodsDataY"
            ref="editCartGoodsDataY"
            class="demo-dynamic"
            label-width="180px"
          >
            <div style="color: #101010; font-weight: bold">百草味旗舰店</div>
            <el-form-item
              prop="bcw.ext.joinList.couponLink"
              label="链接"
              :rules="[
                { required: true, message: '请输入链接', trigger: 'blur' },
              ]"
            >
              <el-input
                placeholder="请输入链接"
                v-model="editCartGoodsDataY.bcw.ext.joinList.couponLink"
                style="width: 63%"
              ></el-input>
            </el-form-item>
            <!-- :before-upload="
                    (file) => {
                      return beforeAvatarUploadConfig(file, 201, 523);
                    }
                  " -->
            <el-form-item
              label="链接图(160*484)"
              :rules="[
                { required: true, message: '请输入链接图', trigger: 'blur' },
              ]"
            >
              <div class="picCommodity">
                <el-upload
                  class="upload-demo"
                  :action="uploadUrl + '/api/v2.1/oss/aliyun/upload'"
                  name="files"
                  :data="{ type: 'add_main_' }"
                  :on-success="uploadSuccessConfig"
                  :headers="myHeaders"
                  :on-error="uploadErrorConfig"
                  multiple
                  :file-list="fileListArea"
                  :show-file-list="false"
                >
                  <img
                    v-show="editCartGoodsDataY.bcw.ext.joinList.imageUrl"
                    :src="editCartGoodsDataY.bcw.ext.joinList.imageUrl"
                    alt
                    style="height: 100px"
                    @click.stop=""
                  />
                  <i
                    v-show="editCartGoodsDataY.bcw.ext.joinList.imageUrl"
                    class="el-icon-error imgDelete"
                    @click.stop="uploadimgDeleteArea1()"
                  ></i>
                  <el-button
                    v-show="!editCartGoodsDataY.bcw.ext.joinList.imageUrl"
                    @click="clickUploadAdd"
                    >点击传图</el-button
                  >
                </el-upload>
              </div>
            </el-form-item>
            <div style="color: #101010; font-weight: bold">
              百事集团官方旗舰店
            </div>
            <el-form-item
              prop="bsjt.ext.joinList.couponLink"
              label="链接"
              :rules="[
                { required: true, message: '请输入链接', trigger: 'blur' },
              ]"
            >
              <el-input
                placeholder="请输入链接"
                v-model="editCartGoodsDataY.bsjt.ext.joinList.couponLink"
                style="width: 63%"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="链接图(160*484)"
              :rules="[
                { required: true, message: '请输入链接图', trigger: 'blur' },
              ]"
            >
              <div class="picCommodity">
                <el-upload
                  class="upload-demo"
                  :action="uploadUrl + '/api/v2.1/oss/aliyun/upload'"
                  name="files"
                  :data="{ type: 'add_main_' }"
                  :on-success="uploadSuccessConfig1"
                  :headers="myHeaders"
                  :on-error="uploadErrorConfig"
                  multiple
                  :file-list="fileListArea"
                  :show-file-list="false"
                >
                  <img
                    v-show="editCartGoodsDataY.bsjt.ext.joinList.imageUrl"
                    :src="editCartGoodsDataY.bsjt.ext.joinList.imageUrl"
                    alt
                    style="height: 100px"
                    @click.stop=""
                  />
                  <i
                    v-show="editCartGoodsDataY.bsjt.ext.joinList.imageUrl"
                    class="el-icon-error imgDelete"
                    @click.stop="uploadimgDeleteArea2()"
                  ></i>
                  <el-button
                    v-show="!editCartGoodsDataY.bsjt.ext.joinList.imageUrl"
                    @click="clickUploadAdd"
                    >点击传图</el-button
                  >
                </el-upload>
              </div>
            </el-form-item>
            <div style="color: #101010; font-weight: bold">百事饮品旗舰店</div>
            <el-form-item
              prop="bsyp.ext.joinList.couponLink"
              label="链接"
              :rules="[
                { required: true, message: '请输入链接', trigger: 'blur' },
              ]"
            >
              <el-input
                placeholder="请输入链接"
                v-model="editCartGoodsDataY.bsyp.ext.joinList.couponLink"
                style="width: 63%"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="链接图(160*484)"
              :rules="[
                { required: true, message: '请输入链接图', trigger: 'blur' },
              ]"
            >
              <div class="picCommodity">
                <el-upload
                  class="upload-demo"
                  :action="uploadUrl + '/api/v2.1/oss/aliyun/upload'"
                  name="files"
                  :data="{ type: 'add_main_' }"
                  :on-success="uploadSuccessConfig2"
                  :headers="myHeaders"
                  :on-error="uploadErrorConfig"
                  multiple
                  :file-list="fileListArea"
                  :show-file-list="false"
                >
                  <img
                    v-show="editCartGoodsDataY.bsyp.ext.joinList.imageUrl"
                    :src="editCartGoodsDataY.bsyp.ext.joinList.imageUrl"
                    alt
                    style="height: 100px"
                    @click.stop=""
                  />
                  <i
                    v-show="editCartGoodsDataY.bsyp.ext.joinList.imageUrl"
                    class="el-icon-error imgDelete"
                    @click.stop="uploadimgDeleteArea3()"
                  ></i>
                  <el-button
                    v-show="!editCartGoodsDataY.bsyp.ext.joinList.imageUrl"
                    @click="clickUploadAdd"
                    >点击传图</el-button
                  >
                </el-upload>
              </div>
            </el-form-item>
            <div style="color: #101010; font-weight: bold">
              quaker桂格旗舰店
            </div>
            <el-form-item
              prop="gg.ext.joinList.couponLink"
              label="链接"
              :rules="[
                { required: true, message: '请输入链接', trigger: 'blur' },
              ]"
            >
              <el-input
                placeholder="请输入链接"
                v-model="editCartGoodsDataY.gg.ext.joinList.couponLink"
                style="width: 63%"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="链接图(160*484)"
              :rules="[
                { required: true, message: '请输入链接图', trigger: 'blur' },
              ]"
            >
              <div class="picCommodity">
                <el-upload
                  class="upload-demo"
                  :action="uploadUrl + '/api/v2.1/oss/aliyun/upload'"
                  name="files"
                  :data="{ type: 'add_main_' }"
                  :on-success="uploadSuccessConfig3"
                  :headers="myHeaders"
                  :on-error="uploadErrorConfig"
                  multiple
                  :file-list="fileListArea"
                  :show-file-list="false"
                >
                  <img
                    v-show="editCartGoodsDataY.gg.ext.joinList.imageUrl"
                    :src="editCartGoodsDataY.gg.ext.joinList.imageUrl"
                    alt
                    style="height: 100px"
                    @click.stop=""
                  />
                  <i
                    v-show="editCartGoodsDataY.gg.ext.joinList.imageUrl"
                    class="el-icon-error imgDelete"
                    @click.stop="uploadimgDeleteArea4()"
                  ></i>
                  <el-button
                    v-show="!editCartGoodsDataY.gg.ext.joinList.imageUrl"
                    @click="clickUploadAdd"
                    >点击传图</el-button
                  >
                </el-upload>
              </div>
            </el-form-item>
            <el-form-item style="margin-top: 40px">
              <el-button type="danger" @click="closeDialog()">取消</el-button>
              <el-button
                type="primary"
                @click="confirmAddConfig('editCartGoodsDataY')"
                >确认</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import requestAxios from "../../../http";
export default {
  name: "configureEditY",
  data() {
    return {
      editCartGoodsData: [],
      fileListArea: [], //图片文件上传列表，定制区域部分
      dialogFormVisible: true, //背景遮罩层
      topTitle: "编辑入会优惠券", //弹出框上面的标题
      uploadUrl: "",
      editCartGoodsDataY: {
        bcw: {
          ext: {
            joinList: {
              imageUrl: "",
              couponLink: "",
            },
          },
        },

        bsjt: {
          ext: {
            joinList: {
              imageUrl: "",
              couponLink: "",
            },
          },
        },

        bsyp: {
          ext: {
            joinList: {
              imageUrl: "",
              couponLink: "",
            },
          },
        },

        gg: {
          ext: {
            joinList: {
              imageUrl: "",
              couponLink: "",
            },
          },
        },
      }, //点击,新增下一个页面要展示出来的数据 //要编辑的列表部分的内容
    };
  },
  props: ["addOrEdit", "editGoodsCont3"],
  created() {
    this.myHeaders = {
      token: sessionStorage.getItem("authorization"),
    };
  },
  mounted() {
    this.uploadUrl = window.uploadUrl;
    this.editCartGoodsDataE = this.$props.editGoodsCont3;
    console.log(this.editCartGoodsDataE, "父传子");
    this.editCartGoodsDataY.bcw.ext = JSON.parse(
      this.editCartGoodsDataE[0].ext
    );
    this.editCartGoodsDataY.bsjt.ext = JSON.parse(
      this.editCartGoodsDataE[1].ext
    );
    this.editCartGoodsDataY.bsyp.ext = JSON.parse(
      this.editCartGoodsDataE[2].ext
    );
    this.editCartGoodsDataY.gg.ext = JSON.parse(this.editCartGoodsDataE[3].ext);
    console.log(this.editCartGoodsDataY, "wwwerr");
  },
  methods: {
    //新增确定调取接口
    confirmAddConfig(formName) {
      console.log(formName, "参数11234");
      this.$refs[formName].validate((valid) => {
        if (
          !this.editCartGoodsDataY.bcw.ext.joinList.imageUrl ||
          !this.editCartGoodsDataY.bsjt.ext.joinList.imageUrl ||
          !this.editCartGoodsDataY.bsyp.ext.joinList.imageUrl ||
          !this.editCartGoodsDataY.gg.ext.joinList.imageUrl
        ) {
          this.$message.error("请完善图片信息！");
          return;
        }
        if (valid) {
          if (this.editCartGoodsDataY.bcw.ext) {
            this.editCartGoodsDataY.bcw.ext = JSON.stringify(
              this.editCartGoodsDataY.bcw.ext
            );
          }
          if (this.editCartGoodsDataY.bsjt.ext) {
            this.editCartGoodsDataY.bsjt.ext = JSON.stringify(
              this.editCartGoodsDataY.bsjt.ext
            );
          }
          if (this.editCartGoodsDataY.bsyp.ext) {
            this.editCartGoodsDataY.bsyp.ext = JSON.stringify(
              this.editCartGoodsDataY.bsyp.ext
            );
          }
          if (this.editCartGoodsDataY.gg.ext) {
            this.editCartGoodsDataY.gg.ext = JSON.stringify(
              this.editCartGoodsDataY.gg.ext
            );
          }
          let obj = this.editCartGoodsDataY;
          console.log(obj, "22222222222");
          requestAxios
            .fetchPost("/api/v2.1/task/config/management/coupons/update", obj, {
              headers: {},
            })
            .then((data) => {
              if (data.status == 200 && data.data.success) {
                console.log("新增兑换商品结果", data.data);
                //新增成功
                this.$emit("getAddGoodsList");
                this.tip(true, "编辑入会优惠券成功!");
                this.$parent.addOrEdit = 0;
              } else {
                this.tip(false, data.data.errorMsg);
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          // 获取图形验证码
          this.$message.error("请完善商品信息！");
          return false;
        }
      });
    },

    // 图片操作
    //新增页面上传图片
    clickUploadAdd() {
      console.log("点击传图");
    },
    //上传图片部分的操作
    beforeAvatarUploadConfig(file, width, height) {
      console.log("上传图片", file);
      // return file;
      return this.beforeAvatarUpload(file, 5, width, height);
    },
    uploadSuccessConfig(response) {
      console.log("图片上传成功", response);
      if (response.success == false) {
        this.tip(false, "请重新登录");
      } else {
        this.editCartGoodsDataY.bcw.ext.joinList.imageUrl =response.data;
        this.tip(true, "图片上传成功!");
      }
    },
    uploadSuccessConfig1(response) {
      console.log("图片上传成功", response);
      if (response.success == false) {
        this.tip(false, "请重新登录");
      } else {
        this.editCartGoodsDataY.bsjt.ext.joinList.imageUrl =response.data;
        this.tip(true, "图片上传成功!");
      }
    },
    uploadSuccessConfig2(response) {
      console.log("图片上传成功", response);
      if (response.success == false) {
        this.tip(false, "请重新登录");
      } else {
        this.editCartGoodsDataY.bsyp.ext.joinList.imageUrl =response.data;
        this.tip(true, "图片上传成功!");
      }
    },
    uploadSuccessConfig3(response) {
      console.log("图片上传成功", response);
      if (response.success == false) {
        this.tip(false, "请重新登录");
      } else {
        this.editCartGoodsDataY.gg.ext.joinList.imageUrl =response.data;
        this.tip(true, "图片上传成功!");
      }
    },
    uploadErrorConfig(err, file, fileListArea) {
      console.log("uploadError", err, file, fileListArea);
      this.tip(false, "图片上传失败!");
    },
    //验证图片
    beforeAvatarUpload(file, size = 5, width = 750, height = 1334) {
      console.log("上传图片的宽高限制", width, height);
      let that = this;
      console.log("验证图片", file.type);
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < size;
      if (!isJPG && !isPNG) {
        this.$message.error("上传图片只能是 PNG/JPG 格式!");
        return false;
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 5MB!");
        return false;
      }

      //判断图片尺寸
      const isSize = new Promise((resolve, reject) => {
        let _URL = window.URL || window.webkitURL;
        let img = new Image();
        img.onload = function () {
          if (width && height) {
            let valid = img.width == width && img.height == height;
            valid ? resolve() : reject();
          } else {
            if (width) {
              let valid = img.width == width;
              valid ? resolve() : reject();
            } else {
              let valid = img.height == height;
              valid ? resolve() : reject();
            }
          }
        };
        img.src = _URL.createObjectURL(file);
      }).then(
        () => {
          that.imgUploadLetterLoding = true;
          return file;
        },
        () => {
          if (width && height) {
            that.$message.error(`上传图片的尺寸必须为 ${width}*${height}`);
          } else {
            if (width) {
              that.$message.error(`上传图片的宽度必须为 ${width}`);
            } else {
              that.$message.error(`上传图片的高度必须为 ${height}`);
            }
          }
          return Promise.reject();
        }
      );
      return isSize;
    },
    // 图片删除
    uploadimgDeleteArea1() {
      this.editCartGoodsDataY.bcw.ext.joinList.imageUrl = "";
      console.log("删除了第几个图片");
    },
    uploadimgDeleteArea2() {
      this.editCartGoodsDataY.bsjt.ext.joinList.imageUrl = "";
      console.log("删除了第几个图片");
    },
    uploadimgDeleteArea3() {
      this.editCartGoodsDataY.bsyp.ext.joinList.imageUrl = "";
      console.log("删除了第几个图片");
    },
    uploadimgDeleteArea4() {
      this.editCartGoodsDataY.gg.ext.joinList.imageUrl = "";
      console.log("删除了第几个图片");
    },

    closeDialog() {
      console.log("关闭弹窗");
      this.$parent.addOrEdit = 0;
    },

    //提示的通用方法
    tip(state, text) {
      if (state) {
        this.$message({
          showClose: true,
          message: text,
          type: "success",
        });
      } else {
        this.$message({
          showClose: true,
          message: text,
          type: "error",
        });
      }
    },
  },
};
</script>
<style>
.inputt {
  width: 63%;
  height: 150px;
  border-radius: 5px;
  display: block;
}
.addGoodsAdd .close_boun {
  position: absolute;
  font-size: 26px;
  /* color: white; */
  top: 15px;
  right: 10px;
}
.configBounDele {
  margin-right: 46px;
}
.el-icon-error {
  margin-left: 50px;
  font-size: 17px;
  color: rgb(133, 131, 131);
}
.enterInput {
  width: 51%;
  outline: none;
  padding-left: 7px;
}
.disApperInput {
  margin-left: 13%;
}
.disApperInput input {
  outline: none;
}
.closeSelect {
  width: 27%;
  height: 22px;
  padding-left: 7px;
  display: inline-block;
  margin-right: 10%;
  position: relative;
  margin-bottom: 10px;
}
.selectListX {
  /* width: 70%; */
  margin-left: 12%;
}
.selectListX .el-icon-close {
  position: absolute;
  top: 5px;
  left: 130px;
}
.selectListX .closeSelect:nth-child(2n) {
  margin-right: 0;
}
.affirmAddCancel .el-button--danger:focus,
.affirmAddCancel .el-button--danger {
  width: 93px;
  background: white;
  color: #d4af82;
  border: 1px solid #d4af82;
  margin-right: 20px;
}
.clickUpload {
  display: block;
  width: 93px;
  color: #d4af82;
  font-size: 12px;
  height: 28px;
  line-height: 28px;
  border-radius: 4px;
  border: 1px solid #d4af82;
  text-align: center;
  margin-left: 22%;
}
.affirmAddCancel {
  margin-top: 40px;
  text-align: center;
}
.affirmAddCancel .el-button--primary {
  width: 93px;
  /* background: #d4af82; */
  border: none;
}

/*
传图页面的样式
*/
.uploadPicList {
  height: 100px;
  display: flex;
}
.picCommodity {
  height: 100px;
  display: inline-block;
  width: 380px;
  position: relative;
}
.uploadPicName {
  display: inline-block;
  margin-left: 100px;
  width: 150px;
  line-height: 100px;
}
.picCommodity .imgDelete {
  /* color: #d4af82; */
  position: absolute;
  left: -66px;
  top: -9px;
}
.picCommodity img {
  position: absolute;
  left: 0px;
  top: 0px;
}
/* 确定和取消按钮 */
.configUploadPage .affirmAddCancel .el-button--danger:focus,
.configUploadPage .affirmAddCancel .el-button--danger {
  width: 93px;
  background: white;
  /* color: #d4af82;
  border: 1px solid #d4af82; */
  margin-right: 20px;
}
.configUploadPage .affirmAddCancel .el-button--primary {
  width: 93px;
  /* background: #d4af82; */
  border: none;
}
.configUploadPage {
  width: 80%;
  margin: 0 auto;
}
</style>
